package login;

import io.ClientSession;
import io.OperationResponse;

import java.util.HashMap;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

public class IosZsy {
	/**
	 * gameID 10038
serverID: M1055
app_key: 4z493v8x5l96bb4zkzl2
syn_key: 6pccj2kxxrhl4kni6c5i
projectID: P10285
	 */
	protected static final Logger log = LoggerFactory.getLogger(IosZsy.class);
	 static String zsyServerUrl = "http://iosrs.tisgame.com/sdksrv/auth/getToken.lg";
     static String zsyServerInfoUrl = "http://iosrs.tisgame.com/sdksrv/auth/getUserInfo.lg";

//	 static String zsyServerUrl = "http://andrs.tisgame.com/andsrv/auth/getToken.lg";
//     static String zsyServerInfoUrl = "http://andrs.tisgame.com/andsrv/auth/getUserInfo.lg";
//     static String zsyGameID = "D10055A";
//     static String zsyServerID = "M1078A";
//     static String zsyApp_key = "ggyepbssk9qamljkoo8k";
//     static String zsySecret_key = "1ufov59xnjijjddk76mq";
//     static String zsySyn_key = "j6ol00h2s7x4pn6393ub";
//     static String zsyProjectID = "P12081A";
     static String zsyGameID = "D10032";
     static String zsyApp_key = "ifk1gdedtbg26urm3yc7";
     static String zsySyn_key = "9ig9slnyc61olsh5uxe9";
	public static  void LoginZSY(OperationResponse operationRequest,ClientSession session) {
		try {
			if(operationRequest.Parameters.size()<2){
				
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			}
			String id = (String) operationRequest.Parameters
					.get(ParameterType.UserID);
			String token = (String) operationRequest.Parameters
					.get(ParameterType.UserPwd);
			String deviceID = (String) operationRequest.Parameters
					.get(ParameterType.DeviceID);
			String isGetIP = (String) operationRequest.Parameters
					.get(ParameterType.IsGetIP);
			String PhoneType=(String) operationRequest.Parameters
					.get(ParameterType.PhoneType);
			if (id.equals("zealm") && token.equals("")) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, AppConfig.dicAppInfo,
						ReturnCode.GetServer, "");
				session.SendOperationResponse(response);
				return;

			}
			HashMap<String, String> dicBack=GetLoginZSY(id);
			if(dicBack==null){
				return;
			}
			 id = "zsy" + dicBack.get("sdkuserid");
			 String  authcode=dicBack.get("authCode");
			 String username=dicBack.get("username");
			 String sdkuserid=dicBack.get("sdkuserid");
			 String access_token=dicBack.get("access_token");
			 String  ids=dicBack.get("id");
			 String zsyBack=String.format("%1$s;%2$s;%3$s;%4$s;%5$s", authcode,username,sdkuserid,access_token,ids);
			 HashMap<Short, Object> parmBack = new HashMap<Short, Object>();
				parmBack.put(ParameterType.ItemID, zsyBack);
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, parmBack,
						ReturnCode.ZSYBack, "");
				session.SendOperationResponse(response);
				operationRequest.Parameters.put(ParameterType.UserID, id);
				HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
				myPrams.put(ParameterType.UserID, id);
				myPrams.put(ParameterType.ItemID, zsyBack);
				OperationResponse responseID= new OperationResponse(
						operationRequest.OperationCode, myPrams,
						ReturnCode.HasID, "");
				session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session,id, deviceID, isGetIP,"zsy",PhoneType,"");
			
		} catch (Exception e) {
		}

	}
	public static HashMap<String, String > GetLoginZSY(	String mCode) throws JSONException{
		if(mCode.isEmpty()){
			return null;
		}
		HashMap<String, String> param=new HashMap<String, String>();
		
		String body="productId="+zsyGameID+"&redirect_uri="+"1"+"&client_id="+zsyApp_key+"&client_secret="+zsySyn_key+"&grant_type="+"authorization_code"+"&code="+mCode;
		String result = Util.sendPost(zsyServerUrl, body);
		
	
		JSONObject token = new JSONObject(result);
		String codes = token.optString("codes");
		
		String accessToken=token.optString("access_token");
		param.put("authCode", mCode);
		param.put("access_token", accessToken);
		if(codes.equals("0")&&!accessToken.isEmpty()){
			
			body="access_token="+accessToken;
			
			
			result=Util.sendPost(zsyServerInfoUrl, body);
			JSONObject token1 = new JSONObject(result);
			String sdkuserid=token1.optString("sdkuserid");
			String id=token1.optString("id");
			String username=token1.optString("username");
			param.put("sdkuserid", sdkuserid);
			param.put("id", id);

			param.put("username", username);

			codes=token1.optString("codes");
			if(codes.equals("0")){
				
				return param;
			}
			
		}
		
		
		
		return null;
	
		
	}
	public  Runnable getLoginZSY(OperationResponse operationResponse, ClientSession session){ 
		class LoginZSY implements Runnable { 
			private OperationResponse t_operationResponse;
			private ClientSession t_session;
			public LoginZSY(OperationResponse operationResponse, ClientSession session)
			{
				t_operationResponse = operationResponse;
				t_session = session;
			}
			
			@Override
			public void run() {
					try {
						LoginZSY(t_operationResponse, t_session);
					} catch (Exception e) {
						log.info("线程" + Thread.currentThread().getName() + " Login任务Error!!!");
					}
				}
        }
	    return new LoginZSY(operationResponse, session); 
	 } 
	
}
